package com.matt.repository.cookbook;

import com.matt.domain.cookbook.RecipeFood;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface RecipeFoodRepository extends JpaRepository<RecipeFood, Long>, JpaSpecificationExecutor<RecipeFood> {

    RecipeFood findFirstByAreaIdAndId(String areaId, Long id);

    RecipeFood findFirstByNameAndAreaId(String foodName, String areaId);

//    @Override
//    public void insertRecipeFoodByTemplate(String areaId) {
//        String query = "insert into tb_recipe_food (name,photo,type,type_name,area_id) " +
//                String.format("select a.name,a.photo,a.type,a.type_name, '%s' area_id ", areaId) +
//                "from tb_recipe_food a where a.area_id = '0' " +
//                String.format("and a.name not in (select name from tb_recipe_food where area_id = '0e5ca75af1274d6ca4c0c2b4c2fde044')", areaId);
//
//        sql(query);
//    }
    @Query(value = "insert into tb_recipe_food (name,photo,type,type_name,area_id) " +
            "select a.name,a.photo,a.type,a.type_name, ?1 as area_id " +
            "from tb_recipe_food a where a.area_id = '0' " +
            "and a.name not in (select name from tb_recipe_food where area_id = ?1)", nativeQuery = true)
    @Modifying
    @Transactional
    int insertRecipeFoodByTemplate(String areaId);

    RecipeFood findByAreaIdAndName(String areaId ,String foodName);
}